<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>事件委托</title>
</head>
<body>
    <div>
        <h3>什么是事件委托？</h3>
        <h5>事件委托就是利用冒泡，只指定一个事件处理程序，让其管理某一类型的所有事件（现有的和新增的）</h5>
        <ul id="ul">
            <li>1</li>
            <li>2</li>
            <li>3</li>
            <li>4</li>
            <li>5</li>
            <li>6</li>
        </ul>
        <button id="btn">添加li</button>
    </div>
    <script>
        let btn = document.getElementById('btn')
        let ul = document.getElementById('ul')
        btn.onclick = () => {
            let li = document.createElement('li')
            let lis = document.getElementsByTagName('li')
            li.textContent = lis.length + 1
            ul.appendChild(li)
        }
        ul.onclick = (event) => {
            event = event || window.event
            let target = event.target
            if (target.nodeName == 'LI') {
                console.log(target.textContent)
            }
        }
    </script>
</body>
</html>